App.controller('salaryPayRollDetailController', ['$rootScope', '$scope', '$state', 'Page', function($rootScope, $scope, $state, Page) {

	// 查询工资单的年份
	 Page.ajaxGet('salaryPayRoll/getSalaryPayRollYear.do', null, function(resp) {
	        $scope.salaryPayRollYearList = resp.data;
	 });
	
    $scope.grid = {
        add : function() {
            $scope.salaryPayRollDetail = {};
            $scope.modal.show();
        },
        update: function(row) {
            $scope.salaryPayRollDetail = row;
           $scope.modal.show();
        },
        save : function() {
            Page.ajaxPost($scope.salaryPayRollDetail.paySlipId ? 'salaryPayRollDetail/update.do' : 'salaryPayRollDetail/add.do', $scope.salaryPayRollDetail, function() {
                $scope.grid.query();
                $scope.grid.cancel();
            });
        },
        column_defs: [
			{
			    target: 0,
			    template: function(row) {
			    	var payrollYear = row.payrollYear;
			    	var payrollMonth = row.payrollMonth;
			    	if(payrollMonth<10){
			    		payrollMonth = "0"+payrollMonth;
			    	}
			    	var salaryMonth = payrollYear+"年"+payrollMonth+"月";
			        return '<font>'+salaryMonth+'</font>'
			    }
			},
          {
            target : 15,
            buttons : [
                {
                    name: '编辑',
                    class: 'btn-warning btm-sm',
                    icon: 'fa-edit',
                    event: 'update',
                    visiable: function(row) {
                        return row.payRollStatus == '1';
                    }
                }
            ]
        }],
        changeSalaryPayRollYear : function(){
    	 // 查询工资单年份对应的月份
    	 var param = {};
    	 param.salaryPayRollYear = $scope.payRollYear;
       	 Page.ajaxGet('salaryPayRoll/getSalaryPayRollMonthByYear.do', param, function(resp) {
       	        $scope.salaryPayRollMonthList = resp.data;
       	    });
        },
        changeValue : function(){// 修改考勤扣款、所得税
        	//实发工资=应发工资+提成-考勤扣款-所得税
        	var payableSalary = $scope.salaryPayRollDetail.payableSalary;// 应发工资
        	var deduction = parseFloat($scope.salaryPayRollDetail.deduction);// 提成
        	var attendanceDeductions = parseFloat($scope.salaryPayRollDetail.attendanceDeductions);// 考勤扣款
        	var tax = parseFloat($scope.salaryPayRollDetail.tax);// 所得税
        	
        	$scope.salaryPayRollDetail.paidSalary = payableSalary + deduction - attendanceDeductions - tax ;
        }
    };


}]);